package com.jingtai.platform.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.common.webfacade.error.ErrorEnum;
import lombok.Data;

@Data
public class ResultUtil {

    private String status;

    private Integer code;

    private String message;

    private Object data;

    private Long count;

    public ResultUtil() {
    }

    public ResultUtil(String message) {
        this.status = "FAIL";
        this.code = 401;
        this.message = message;
    }

    public ResultUtil(ResultMapInfo errorEnum) {
        this.status = errorEnum.getCode() == 1?"SUCCESS":"FAIL";
        this.message = errorEnum.getMessage();
    }

    public ResultUtil(ErrorEnum errorEnum) {
        this.status = errorEnum.getCode() == 1?"SUCCESS":"FAIL";
        this.message = errorEnum.getErrorDescription();
    }

    public ResultUtil(ResultMapInfo errorEnum,Object data) {
        this.status = errorEnum.getCode() == 1?"SUCCESS":"FAIL";
        this.code = 0;
        this.message = errorEnum.getMessage();
        if (data instanceof Page){
            Page page = (Page) data;
            this.data = page.getRecords();
            this.count = page.getTotal();
        }else {
            this.data = data;
        }
    }

    public ResultUtil(ErrorEnum errorEnum,Object data) {
        this.status = errorEnum.getCode() == 1?"SUCCESS":"FAIL";
        this.code = 0;
        this.message = errorEnum.getErrorDescription();
        this.data = data;
    }



    @Override
    public String toString() {
        return "ResultUtil{" +
                "status='" + status + '\'' +
                ", code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
